<?php
class Pages extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('pages_model');
		$this->load->language('pages','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__pages__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$view                     = $this->getPath(__FUNCTION__);
		$view['title']		      = getLangText('pages_listing_title',$this);
		$view['subtitle']	      = getLangText('pages_listing_sub_title',$this);
		$where                    = array('deleted' => 0);
		/*if(isset($_POST['search']))
		{
			$code                            = $this->input->post('code'); 
			if(!empty($code))              // filter subject with course 
			{
				$view['filter']['code']    = $code;  
				$where['pages_code']         = $view['filter']['code'];
			}
			//name filter
			$name						     = $this->input->post('st_name');
			if(!empty($name))
			{
				$view['filter']['name']      = $name;
				$this->session->set_userdata(array('student_filter1' => $name ));
				
			}
			$designation						     = $this->input->post('designation');
			if(!empty($designation))
			{
				$view['filter']['designation']      = $designation;
				$this->session->set_userdata(array('student_filter2' =>  $designation));
				
			}
			$mobile                            = $this->input->post('mobile'); 
			if(!empty($mobile))              // filter subject with course 
			{
				$view['filter']['mobile']    = $mobile;  
				$where['mobile_no']         = $view['filter']['mobile'];
			}
		}
		else
		{
			$view['filter']['name']             = $this->session->userdata('student_filter1');
			$view['filter']['designation']      = $this->session->userdata('student_filter2');
		}	
		if(!empty($view['filter']['name']))
		{
			//$where['course_id']     = $view['filter']['course'];   //$course;
		    $or_like['first_name']              = $view['filter']['name']; 
			$or_like['last_name']           = $view['filter']['name']; 
		}
		if(!empty($view['filter']['designation']))
		{
		    $like['designation']              = $view['filter']['designation']; 
 
		}  */
		
		$per_page_count            = $this->config->item('per_page_count');
		$pages                     = $this->pages_model->getpages($page, $per_page_count, $where);
		
		$view['pages'] 		       = $pages->records;
		
	
		$view['cur_page'] 	       = $page;
		
		/************************* Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/pages/index/';
		$config['total_rows'] 	  = $pages->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment']    = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('page_msg'));
		$this->layout->adminview('page/page_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                   = $this->getPath(__FUNCTION__);
		$view['title']          = getLangText('pages_add_title',$this);
		$view['subtitle']	    = getLangText('pages_add_sub_title',$this);
		$save                   = $this->input->post('save');
		$view['pages']        	= array();
		$view['cur_page']       = $cur_page;
		
		if(!empty($save)) 
		{
			$data = $this->retrieve->post($this->pages_model->fields);
			$insertStatus = $this->products_model->add($data);
			$view['pages'] 	= $data;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('pages_msg', 'pages_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base_url').'pages/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('pages_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('pages_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('pages_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('pages_add_failure');
					break;
			}
		} 
		$cats                      = $this->categories_model->getCategs(0,'', array('deleted'=>0));
		$view['cats'] 		      = $cats->records;
		$this->config->load('myconfig','config');    //fetching value for blood group
		$view['blood_groups']		= $this->config->item('blood_groups');
		$this->layout->adminview('product/product_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->pages_model->getpages(0,1, array('id' => $id));
			$page = $records->records[0];
			if(empty($page)) 
			{
				$this->session->set_flashdata('pages_msg', 'pages_invalid_record');
				redirect($this->config->item('admin_base_url').'page/index','location');
				exit;
			}
			$view['id']		    = $id;
			
			$view['page']	    = $page;
		}
		
		$view['title']          = "Edit pages ";
		$view['page_title']     = "Edit pages";
		$view['subtitle']	    = 'Edit pages';
		$save                   = $this->input->post('save');
		if(!empty($save)) 
		{
			$data               = $this->retrieve->post($this->pages_model->fields);
			$insertStatus       = $this->pages_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('page_msg', 'page_edit_success');
					redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('page_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('page_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('page_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('page_edit_failure');
					break;
			}
			$view['pages'] 	         = $data;
		}
		$view['cur_page']            = $cur_page; 
		$this->config->load('myconfig','config');    //fetching value for blood group
		$view['blood_groups']		= $this->config->item('blood_groups');
		$this->layout->adminview('page/page_addedit', $view);
	}
	function view($id = '',$cur_page=''){
		$where= array('deleted'=>0);
		
		
		
		$page          = $this->pages_model->getPages(0,1, array('id' => $id));
		
		$view['page'] 		= $page->records[0];
		
		
		$this->load->view('admin/page/page_info', $view);
	}
	function cancel()
	{
    redirect($this->config->item('admin_base_url').'pages/index','location');
		exit;	
	}
	function export($id, $cur_page=0) 
	{
		if(empty($id)) 
		{
		redirect($this->config->item('admin_base').'pages/index/'.$cur_page,'location');
		exit;
		} 
		else 
		{
			$records = $this->pages_model->getPages(0,1, array('id' => $id));
			$pages = $records->records[0];
			$status = $this->pages_model->export($pages['content'], $pages['filename']);
			if($status) 
			{
			
			$this->session->set_flashdata('page_msg', 'page_export_success');
			redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
			exit;
			} else 
			{
			$this->session->set_flashdata('page_msg', 'page_export_failure');
			redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
			}
			//redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
			//exit;
		}

	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus                = $this->products_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'pages_delete_success');
					redirect($this->config->item('admin_base_url').'pages/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	      = $this->lang->line('pages_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('student_filter1');
		$this->session->unset_userdata('student_filter2');
		redirect($this->config->item('admin_base_url').'pages/index/','location');
	}
}
?>